package com.dbpediamobile;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

/*
 * Autor: Camilo Verdugo
 * Clase que realiza una consulta simple al inicio de la aplicacion para comprobar si el endpoint esta online, de caso contrario
 * retorna un string vacio, el cual indica que el sistema esta temporalmente down
 */

public class CheckConexion {

	public CheckConexion(){}

	public String check() throws IOException{
		
	    String query = "select+distinct+%3FConcept+where+{[]+a+%3FConcept}+limit+1";								
	    String u = "http://dbpedia.org/sparql?default-graph-uri=http://dbpedia.org&should-sponge=&query="+query+"&format=text/html&debug=off&timeout=1000";
		URL dir = new URL(u);
		
        URLConnection yc = dir.openConnection();
        yc.setConnectTimeout(2000);
        BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
        String inputLine;
        
		String salida = "";
	    while ((inputLine = in.readLine()) != null){
	    	salida += inputLine;	
	    }	      
	    in.close();
	    return salida; 
    
	}
	
}
